//
//  AppDelegate.swift
//  weibo_swift
//
//  Created by 紫荆秋雪 on 16/11/27.
//  Copyright © 2016年 紫荆秋雪. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking
/// iOS 10以后为了保护用户隐私，需要程序启动时弹出提醒用户是否接受通知（.alert/.badge/.carPlay/.sound）
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        //1、实例化window
        window = UIWindow()
        //2、设置窗口的背景颜色
        window?.backgroundColor = UIColor.white
        //3、设置 根控制器
        window?.rootViewController = RevanMainViewController()
        //4、显示窗口并且设置为主窗口
        window?.makeKeyAndVisible()
        
        //5、配置信息
        loadAppInfo()
        //6、引用程序额外信息
        setupAdditions()
        
        
        return true
    }
}

// MARK: - 配置信息
extension AppDelegate {
    func loadAppInfo() {
        // 1、模拟异步
        DispatchQueue.global().async {
            // 1> 资源url
            guard let url = Bundle.main.url(forResource: "main_asyn.json", withExtension: nil) else {
                return
            }
            
            // 2> data二进制数据
            let data = NSData(contentsOf: url)
            // 3> 沙盒路径
            let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath  = (path as NSString).appendingPathComponent("main.json")
            // 4> 数据写入沙盒
            data?.write(toFile: jsonPath, atomically: true)
            print("\(jsonPath)")
        }
    }
}

// MARK: - 引用程序的额外设置
extension AppDelegate {
    func setupAdditions() {
        // 1、设置 SVProgressHUD 显示最小解除时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        // 2、设置网络加载显示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        // 3、用户隐私提醒框
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .carPlay, .sound]) { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            }
        } else {
            // 10.0 以下
            // 取得用户授权显示通知【上方的提示条/声音/BadgeNumBer】
            let notifiSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifiSettings)
        }

        
    }
}

